'use strict';
const Controller = require('egg').Controller;

class CalendarController extends Controller {
    async read() {
        const { ctx, app } = this;

        const userid = ctx.request.query.userid;
        let data = [];
        const res = await app.mysql.select('daily', {
            where: { userid: userid },
            columns: ['create_time', 'total_score', 'diary'],
        });

        if(res) {
            data = res.map((item) => {
                return {
                    date: new Date(item.create_time).toISOString().slice(0,10),
                    info: item.total_score + '分',
                    data: {
                        content: `情绪能量：${item.total_score}分，${item.diary}`
                    }
                }
            })
            console.log(data);
            ctx.body = {
                code: 200,
                message: '查询成功',
                data: data
            }
        } else {
            ctx.body = {
                code: 500,
                message: '暂无数据',
                data: []
            }
        }
    }
}

module.exports = CalendarController;